package com.diandian.backend.client.service;

import com.diandian.base.boot.starter.exception.DefaultException;
import com.diandian.base.boot.starter.response.PageResult;
import com.diandian.backend.client.domain.ClientClientPermission;
import com.diandian.backend.client.dto.*;
import com.diandian.backend.client.mapper.ClientPermissionMapper;
import com.diandian.backend.client.resp.ClientClientPermissionDetailResp;
import com.diandian.backend.client.resp.ClientClientPermissionPageResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.diandian.backend.client.resp.ClientClientPermissionListResp;

import java.util.ArrayList;
import java.util.List;

@Service
public class ClientPermissionService {

    @Autowired
    private ClientPermissionMapper clientPermissionMapper;

    public PageResult page(ClientClientPermissionPageParamDTO requestDTO){

        requestDTO.check();
        int pageNum = requestDTO.getPageNum();
        int pageSize = requestDTO.getPageSize();

        // 查询总数
        int totalCount = clientPermissionMapper.getCount(requestDTO);

        // 查询数据集合
        List<ClientClientPermission> dataList = clientPermissionMapper.getPage(requestDTO, requestDTO.getPageSize(), (requestDTO.getPageNum() - 1) * requestDTO.getPageSize());

        // 组装数据
        List<ClientClientPermissionPageResp> respList = new ArrayList<>();
        if(dataList != null && dataList.size() > 0){
            dataList.stream().forEach( x ->{
                ClientClientPermissionPageResp item = new ClientClientPermissionPageResp();
                item.setId(x.getId());
                item.setClientId(x.getClientId());
                item.setPermissionId(x.getPermissionId());
                item.setRemark(x.getRemark());
                item.setStatus(x.getStatus());
                item.setDel(x.getDel());
             respList.add(item);
            });
        }

        return new PageResult(pageNum, pageSize, totalCount, respList);
    }

    public List<ClientClientPermissionListResp> getList(ClientClientPermissionListParamDTO requestDTO){

        // 查询数据集合
        List<ClientClientPermission> dataList = clientPermissionMapper.getList(requestDTO);

        // 组装数据
        List<ClientClientPermissionListResp> respList = new ArrayList<>();
        if(dataList != null && dataList.size() > 0){
            dataList.stream().forEach( x ->{
                ClientClientPermissionListResp item = new ClientClientPermissionListResp();
                item.setId(x.getId());
                item.setClientId(x.getClientId());
                item.setPermissionId(x.getPermissionId());
                item.setRemark(x.getRemark());
                item.setStatus(x.getStatus());
                item.setDel(x.getDel());
                respList.add(item);
            });
        }

        return respList;
    }

    public ClientClientPermissionDetailResp getDetail(ClientClientPermissionDetailParamDTO param) {

        ClientClientPermission dbData = clientPermissionMapper.getDetail(param);
        if(dbData == null){
            return null;
        }

        ClientClientPermissionDetailResp result = new ClientClientPermissionDetailResp();
        result.setId(dbData.getId());
        result.setClientId(dbData.getClientId());
        result.setPermissionId(dbData.getPermissionId());
        result.setRemark(dbData.getRemark());
        result.setStatus(dbData.getStatus());
        result.setDel(dbData.getDel());
        return result;
    }

    public void updateById(ClientClientPermissionUpdateByIdParamDTO param) {
        param.check();

        ClientClientPermissionDetailParamDTO queryDetailDTO = new ClientClientPermissionDetailParamDTO();
        queryDetailDTO.setId(param.getId());
        ClientClientPermission dbData = clientPermissionMapper.getDetail(queryDetailDTO);
        if(dbData == null){
            throw new DefaultException("更新的记录不存在");
        }

        clientPermissionMapper.updateById(param);
    }

//    public void deleteById(ClientClientPermissionDeleteByIdParamDTO paramDTO) {
//        paramDTO.check();
//        clientPermissionMapper.deleteById(paramDTO);
//    }

    public void delete(String clientId, Long permissionId, String remark) {

        // 校验是否已经存在
        ClientClientPermissionDetailParamDTO queryDetailDTO = new ClientClientPermissionDetailParamDTO();
        queryDetailDTO.setClientId(clientId);
        queryDetailDTO.setPermissionId(permissionId);
        ClientClientPermissionDetailResp detailResp = getDetail(queryDetailDTO);
        if(detailResp == null){
            throw new DefaultException("未配置此权限");
        }

        ClientClientPermissionDeleteByIdParamDTO deleteByIdParamDTO = new ClientClientPermissionDeleteByIdParamDTO();
        deleteByIdParamDTO.setId(detailResp.getId());
        clientPermissionMapper.deleteById(deleteByIdParamDTO);

    }

    public void deleteByClientId(String clientId) {
        clientPermissionMapper.deleteByClientId(clientId);
    }

    public void add(ClientClientPermissionAddParamDTO param) {

        param.check();

        // 校验是否已经存在
        ClientClientPermissionDetailParamDTO queryDetailDTO = new ClientClientPermissionDetailParamDTO();
        queryDetailDTO.setClientId(param.getClientId());
        queryDetailDTO.setPermissionId(param.getPermissionId());
        ClientClientPermissionDetailResp detailResp = getDetail(queryDetailDTO);
        if(detailResp != null){
            throw new DefaultException("请不要重复添加");
        }

        param.setDel(0);
        param.setStatus("0");
        clientPermissionMapper.add(param);

    }

    /**
     * 查询某个客户端id端权限集合
     * @param clientId
     * @return
     */
    public List<String> getPermissions(String clientId) {
        return clientPermissionMapper.getPermissions(clientId);
    }
}
